package com.parkreport.report.action;

import java.io.UnsupportedEncodingException;

import javax.xml.rpc.ServiceException;

import org.jfree.data.time.Day;

import com.common.report.vo.ReportTableViewVO;
import com.common.util.CalendarUtil;
import com.common.util.StringUtil;
import com.parkreport.base.BaseAction;
import com.parkreport.report.service.AreaCarNextReportService;
import com.parkreport.report.service.AreaCarReportService;
import com.parkreport.report.service.AreaPersonNextReportService;
import com.parkreport.report.service.AreaPersonReportService;
import com.parkreport.vo.ReportParamVO;

public class AreaReportAction extends BaseAction {

	private AreaCarReportService carService = new AreaCarReportService();
	private AreaPersonReportService personService = new AreaPersonReportService();
	private AreaCarNextReportService carNextService = new AreaCarNextReportService();
	private AreaPersonNextReportService personNextService = new AreaPersonNextReportService();
	
	private ReportParamVO paramVO = new ReportParamVO();
	
	private ReportTableViewVO tableVO = new ReportTableViewVO();
	
	private String searchTitles;
	private String searchMents;
	private String startDate;
	private String endDate;
	
	private String date;

	private String typeStr = "";
	public String getDate() {
		
		if("auto".equals(dateType)){
			if(StringUtil.isBlank(startDate)&&StringUtil.isBlank(endDate)){
				CalendarUtil calUtil = new CalendarUtil();
				date = calUtil.getNowTime("yyyy-MM-dd");
				startDate =date;
				endDate = date;
				
			}
			typeStr = startDate+"至"+endDate;
		}else if("day".equals(dateType)){
			if(StringUtil.isBlank(date)){
				CalendarUtil calUtil = new CalendarUtil();
				date = calUtil.getPreviousDay();
			}
			typeStr = date;
		}else if("month".equals(dateType)){
			
			if(StringUtil.isBlank(date)){
				CalendarUtil calUtil = new CalendarUtil();
				date = calUtil.getPreviousMonth();
			}
			typeStr = date;
		}else if("year".equals(dateType)){
			
			if(StringUtil.isBlank(date)){
				CalendarUtil calUtil = new CalendarUtil();
				date = calUtil.getPreviousYear();
			}
			typeStr = date;
		}
		
		
		
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String personCount(){
		
		try {
			personService.setDateType(dateType);
			personService.setDate(getDate());
			personService.setStartDate(startDate);
			personService.setEndDate(endDate);
			tableVO = personService.getTableViewVO(this.getRequest());
			tableVO.setMentName("来源区域");
			tableVO.setReportName(typeStr+"入境游客数统计");
			this.getSession().setAttribute("tableVO", tableVO);
		} catch (ServiceException e) {
			log.error(e.getMessage(),e);
		}
		
		return topager("/pages/report/area/person_list.jsp");
	}
	
	public String personNextCount(){
		personNextService.setTitles(StringUtil.decodeUTF8(searchTitles));
		personNextService.setMents(StringUtil.decodeUTF8(searchMents));
		try {
			personNextService.setDateType(dateType);
			personNextService.setDate(getDate());
			personNextService.setStartDate(startDate);
			personNextService.setEndDate(endDate);
			tableVO = personNextService.getTableViewVO(this.getRequest());
			tableVO.setMentName("来源区域");
			tableVO.setReportName(typeStr+"入境游客数统计");
			this.getSession().setAttribute("tableVO1", tableVO);
		} catch (ServiceException e) {
			log.error(e.getMessage(),e);
		}
		
		return topager("/pages/report/area/person_list_next.jsp");
	}
	
	public String carCount(){
		
		try {
			carService.setDateType(dateType);
			carService.setDate(getDate());
			carService.setStartDate(startDate);
			carService.setEndDate(endDate);
			tableVO = carService.getTableViewVO(this.getRequest());
			tableVO.setMentName("来源区域");
			tableVO.setReportName(typeStr+"入境游客车辆数统计");
			this.getSession().setAttribute("tableVO", tableVO);
		} catch (ServiceException e) {
			log.error(e.getMessage(),e);
		}
		
		return topager("/pages/report/area/car_list.jsp");
	}
	
	public String carNextCount(){
		
		log.info("searchTitles======="+StringUtil.decodeUTF8(searchTitles));
		carNextService.setTitles(StringUtil.decodeUTF8(searchTitles));
		carNextService.setMents(StringUtil.decodeUTF8(searchMents));
		try {
			carNextService.setDateType(dateType);
			carNextService.setDate(getDate());
			carNextService.setStartDate(startDate);
			carNextService.setEndDate(endDate);
			tableVO = carNextService.getTableViewVO(this.getRequest());
			tableVO.setMentName("来源区域");
			tableVO.setReportName(typeStr+"入境游客车辆数统计");
			this.getSession().setAttribute("tableVO1", tableVO);
		} catch (ServiceException e) {
			log.error(e.getMessage(),e);
		}
		
		return topager("/pages/report/area/car_list_next.jsp");
	}
	
	
	//setter getter 
	public ReportParamVO getParamVO() {
		return paramVO;
	}

	public void setParamVO(ReportParamVO paramVO) {
		this.paramVO = paramVO;
	}

	
	
	public AreaCarReportService getCarService() {
		return carService;
	}

	public void setCarService(AreaCarReportService carService) {
		this.carService = carService;
	}

	public ReportTableViewVO getTableVO() {
		return tableVO;
	}

	public void setTableVO(ReportTableViewVO tableVO) {
		this.tableVO = tableVO;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getSearchMents() {
		return searchMents;
	}
	public void setSearchMents(String searchMents) {
		this.searchMents = searchMents;
	}
	public String getSearchTitles() {
		return searchTitles;
	}
	public void setSearchTitles(String searchTitles) {
		this.searchTitles = searchTitles;
	}
}
